package com.srwl.mytx.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.ChatActivity;
import com.srwl.mytx.activity.TaskDetailActivity;
import com.srwl.mytx.domain.Task;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.utils.DateUtils;
import com.srwl.mytx.utils.GlideLoadUtils;

import java.util.List;

/**
 * 主题任务领取记录适配器
 */
public class TaskAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "BalanceStatementAdapter";
    private List<Task> statementList;
    private boolean bol_noData;//标记是否有数据
    private boolean bol_requestError;//标记是否请求出错
    private Context mContext;

    public TaskAdapter(Context context, List<Task> couponList) {
        mContext = context;
        this.statementList = couponList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            return new NoDataViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.no__data__layout, parent, false));
        } else if (viewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            return new RequestErrorViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.request_error_layout, parent, false));
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.task_item, parent, false);

        return new ItemViewHolder(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        int viewType = getItemViewType(position);
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            NoDataViewHolder noDataViewHolder = (NoDataViewHolder) holder;
            noDataViewHolder.tv_tips.setText("没有记录");
        } else if (viewType == Constant.ITEM_TYPE_NORMAL) {
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            setItemViews(itemViewHolder, position);
        }
    }


    private void setItemViews(ItemViewHolder itemViewHolder, int position) {
        Task task = statementList.get(position);
        User targetUser = task.getTargetUser();
        itemViewHolder.tvDate.setText(DateUtils.getTimestampString(task.getCreated()));
        Integer status = task.getStatus();
        if (status == Constant.TASK_STATUS_CHECK_SUCCESS || status == Constant.TASK_STATUS_EXPIRY_AUTO_FINISH) {
            itemViewHolder.tv_status.setText("[已完成]");
            itemViewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.rp_money_red_light));
        }
        if (status >= Constant.TASK_STATUS_CONFIRM) {
            itemViewHolder.tv_status.setText("[执行中]");
            itemViewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.rp_money_red_light));
        } else {
            itemViewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.text_gray));
            if (status == Constant.TASK_STATUS_CONFIRM_FAIL) {
                itemViewHolder.tv_status.setText("[已否决]");
            } else if (status == Constant.TASK_STATUS_INIT) {
                itemViewHolder.tv_status.setText("[待确认]");
            } else if (status == Constant.TASK_STATUS_EMPLOYEE_CANCEL || status == Constant.TASK_STATUS_EMPLOYEE_AGREE_CANCEL || status == Constant.TASK_STATUS_EXPIRY_AUTO_CANCEL) {
                itemViewHolder.tv_status.setText("[已取消]");
            }
        }
        itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TaskDetailActivity.startActivity((Activity) mContext, task, 1);
            }
        });
        if (targetUser != null) {
            itemViewHolder.tv_title.setText(targetUser.getUsername() != null ? targetUser.getUsername() : targetUser.getNick());
            //Glide.with(mContext).load(targetUser.getAvatar()).into(itemViewHolder.ivAvatar);
            GlideLoadUtils.glideLoad(mContext, targetUser.getAvatar(), itemViewHolder.ivAvatar);
            itemViewHolder.btn_contact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ChatActivity.startActivity(mContext, targetUser);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (bol_noData || bol_requestError) {
            return 1;
        }
        return statementList.size();
    }


    @Override
    public int getItemViewType(int position) {
        if (bol_noData) {
            return Constant.ITEM_TYPE_NO_DATA;
        } else if (bol_requestError) {
            return Constant.ITEM_TYPE_REQUEST_ERROR;
        }
        return Constant.ITEM_TYPE_NORMAL;
    }


    /**
     * 显示普通视图
     * 有数据的时候调用
     */
    public void showNormalView() {
        bol_noData = false;
        bol_requestError = false;
        notifyDataSetChanged();
    }

    /**
     * 显示无数据视图
     */
    public void showNoDataView() {
        bol_noData = true;
        notifyDataSetChanged();
    }

    /**
     * 显示请求数据错误视图
     */
    public void showErrorView() {
        bol_requestError = true;
        notifyDataSetChanged();
    }


    private static class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        TextView tvGreeting;
        TextView tvMoneyAmount;
        TextView tvMoneyStatus;
        ImageView ivAvatar;
        TextView tvMoneyUse;
        ImageView ivGroupIcon;

        HeaderViewHolder(View v) {
            super(v);
            initView(v);
        }

        private void initView(View v) {
            tv_title = v.findViewById(R.id.tv_money_sender);
            tvGreeting = v.findViewById(R.id.tv_greeting);
            tvMoneyStatus = v.findViewById(R.id.tv_money_status);
            tvMoneyAmount = v.findViewById(R.id.tv_money_amount);
            ivAvatar = v.findViewById(R.id.iv_avatar);
            tvMoneyUse = v.findViewById(R.id.tv_money_use);
            ivGroupIcon = v.findViewById(R.id.iv_group_random);
        }
    }

    private static class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAvatar;
        TextView tv_title;
        TextView tvDate;
        TextView btn_contact;
        TextView tv_status;

        ItemViewHolder(View v) {
            super(v);
            initView(v);
        }

        private void initView(View v) {
            ivAvatar = v.findViewById(R.id.iv_item_avatar_icon);
            tv_title = v.findViewById(R.id.tv_title);
            tvDate = v.findViewById(R.id.tv_time);
            btn_contact = v.findViewById(R.id.btn_contact);
            tv_status = v.findViewById(R.id.tv_status);
        }
    }


}
